<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
    <title>美策平台</title>
    <link rel="icon" type="image/png" href="/img/icon.png">
    <link rel="stylesheet" type="text/css" href="../../datepicker/jquery-ui.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/app.css">
    <link rel="stylesheet" type="text/css" href="../../css/index.css">
    <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
    <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/framework7/1.6.5/css/framework7.ios.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/framework7/1.6.5/css/framework7.ios.colors.min.css" rel="stylesheet">

    <script type="text/javascript" src="/h5/js/all.js"></script><style>
        table { width:100%; }
        th,td { text-align:center;border:1px solid #f0f0f0; }
        th { padding:10px 0;background:#f9f9f9; }
        td input { padding:10px 0;font-size:14px;border:0;text-align:center; }
        td input.title { width:100%; }
        #popDetail { text-align:center; }
        #popDetail input { padding:5px;
            border:1px solid #0088cc;text-align:center }
        #popDetail input[type=button] {
            margin-top:10px;padding:5px 20px;
            background:#0088cc;color:#fff;font-size:16px;
        }
        #popDetail input.close { background:#555555;border:0; }
        /*末尾指引*/
        #zhiyin { width:100%;height:40px;
            border:0;line-height:40px;color:#aaaaaa;
            background:#e0e0e0;text-align:center;
            position:fixed;bottom:0;
        }
        #zhiyin button { padding:5px 10px;background:#fff;border:0; }
        #zhiyin .btnCurr { color:#fff;background:#0088cc; }
    </style>
</head>
<body style="overflow-y: auto;-webkit-overflow-scrolling: touch">
<!--
    制作者：九哥
    页面：发布订单页面
    时间：2019-05-24
-->





<!----------发布订单页面---------->
<div id="addOrderPage"><!--此ID页面切换用-->
    <div id="addOrderTitle">
        <b class="title">原料面料清单</b>
        <img src="../../images/return.png"
             id="addOrderRtn" onclick="history.go(-1)">
    </div>
    <!--<div style="height:1.593333rem"></div>-->
    <div style="height:40px;"></div>
    <div id="tlist">
        <!--<table cellspacing="0">-->
            <!--<tr>-->
                <!--<td colspan="2">-->
                    <!--&lt;!&ndash;面辅料单耗&ndash;&gt;-->
                    <!--<input type="text" class="title"-->
                           <!--placeholder="新表名称，点击编辑" readonly-->
                           <!--onclick="addType()">-->
                <!--</td>-->
            <!--</tr>-->
            <!--<tr>-->
                <!--<th>名称</th>-->
                <!--<th>单价</th>-->
            <!--</tr>-->
            <!--<tr>-->
                <!--<td>-->
                    <!--<input type="text" id="nameNew" placeholder="新名称"/>-->
                <!--</td>-->
                <!--<td class="right">-->
                    <!--<input type="text" id="numNew" placeholder="新单价"/>-->
                <!--</td>-->
            <!--</tr>-->
            <!--<tr>-->
                <!--<th colspan="2"-->
                    <!--style="text-align:center;color:#0088cc;"-->
                    <!--onclick="addRow(0)">保存并增加新行</th>-->
            <!--</tr>-->
        <!--</table>-->
    </div>
    <br><br>
    <table cellspacing="0">
        <tr>
            <th colspan="2"
                style="text-align:center;color:#0088cc;"
                onclick="addType()">
                <b>增加新单</b>
            </th>
        </tr>
    </table>
    <div id="addOrderDetail">
        <p id="addOrderSubmit">
            <button onclick="setNext()">设置订单合同</button>
        </p>
    </div>
    <!--末尾指引-->
    <div style="height:50px;"></div>
    <div id="zhiyin">
        <button class="btnCurr">订单资料</button>
        --
        <button class="btnCurr">面料清单</button>
        --
        <button>订单合同</button>
    </div>
</div>
<input type="hidden" id="orderId" value="0">
<input type="hidden" id="nextSubmit" value="0">
<input type="hidden" id="typeId" value="0">
<!----------发布订单页面---------->





<!----------弹窗---------->
<div id="pop">
    <div id="popMask"></div>
    <div id="popDetail">
        <!--<input type="text" placeholder="设置分类名称">-->
        <!--<input type="button" class="close" value="取消">-->
        <!--&nbsp; <input type="button" value="确定设置">-->
    </div>
</div>
<!----------弹窗---------->


<!----------错误提示---------->
<div id="errMsg"></div>
<!----------错误提示---------->
</body>
<script type="text/javascript" src="../../js/app.js"></script>
<script type="text/javascript" src="../../js/index.js"></script>
<script>
    (function(){
        $('#orderId')[0].value = getUrlParam('id');
        getTypeList();
    })();
    /**
     * 设置跳转链接
     * */
    function setNext(){
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        var orderId = $('#orderId').val();
        var nextSubmit = $('#nextSubmit').val();
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            orderId:orderId
        };
        $.ajax({
            type:'post',
            url: getDomain() + 'fabric/status-set',
            data:data,
            success: function (res) {
                //console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                getAppErrMsg('更新成功！','',0);
                setTimeout(function() {
                    window.location.href='contract.html?id='+orderId+'&nextSubmit='+nextSubmit;
                }, 1000)
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 新增type
     * */
    function addType(){
        var h = '';
        h += '<input type="text" id="typeName" placeholder="设置分类名称">';
        h += '<input type="button" class="close" value="取消" onclick="$(\'#pop\').hide()">';
        h += '&nbsp; <input type="button" value="确定设置" onclick="addTable()">';
        $('#popDetail').html(h);
        $('#pop').show();
    }
    /**
     * 编辑type
     * */
    function editType(typeId){
        var typeName = $('#typeName'+typeId).val();
        var h = '';
        h += '<input type="text" id="typeName" placeholder="设置分类名称" value="'+typeName+'">';
        h += '<input type="button" class="close" value="取消" onclick="$(\'#pop\').hide()">';
        h += '&nbsp; <input type="button" value="确定设置" onclick="editTable('+typeId+')">';
        $('#popDetail').html(h);
        $('#pop').show();
    }
    /**
     * 编辑costName
     * */
    function editCostName(costId){
        var subName = $('#subName_'+costId).val();
        var h = '';
        h += '<input type="text" id="subName" placeholder="设置面料名称" value="'+subName+'">';
        h += '<input type="button" class="close" value="取消" onclick="$(\'#pop\').hide()">';
        h += '&nbsp; <input type="button" value="确定设置" onclick="setCostName('+costId+')">';
        $('#popDetail').html(h);
        $('#pop').show();
    }
    /**
     * 编辑costNum
     * */
    function editCostNum(costId){
        var subNum = $('#subNum_'+costId).val();
        var h = '';
        h += '<input type="text" id="subNum" placeholder="设置面料名称" value="'+subNum+'">';
        h += '<input type="button" class="close" value="取消" onclick="$(\'#pop\').hide()">';
        h += '&nbsp; <input type="button" value="确定设置" onclick="setCostNum('+costId+')">';
        $('#popDetail').html(h);
        $('#pop').show();
    }
    /**
     * 增加新表
     * */
    function addTable(){
        var orderId = $('#orderId').val();
        var typeName = $('#typeName').val();
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        if (typeName=='') {
            getAppErrMsg('新分类未填写!','',1000);
            return;
        }
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            orderId:orderId,
            typeName:typeName
        };
        $.ajax({
            type:'post',
            url: getDomain() + 'fabric/type/add',
            data:data,
            success: function (res) {
//                console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                getAppErrMsg('修改成功！','',0);
                setTimeout(function() {
                    getTableAdd(res.data.typeId,typeName);
                }, 1000)
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 编辑新表
     * */
    function editTable(typeId){
        var orderId = $('#orderId').val();
        var typeName = $('#typeName').val();
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        if (typeName=='') {
            getAppErrMsg('新分类未填写!','',1000);
            return;
        }
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            orderId:orderId,
            typeId:typeId,
            typeName:typeName
        };
        $.ajax({
            type:'post',
            url: getDomain() + 'fabric/type/modify',
            data:data,
            success: function (res) {
//                console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                getAppErrMsg('新增成功！','',0);
                setTimeout(function() {
                    var h = '';
                    h += '<span style="font-size:20px;" onclick="editType('+typeId+')">'+typeName+'</span>';
                    h += '<input type="hidden" id="typeName'+typeId+'" value="'+typeName+'">';
                    $('#pop').hide();
                    $('#typeSpan'+typeId).html(h);
                }, 1000)
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 表更新
     * */
    function getTableAdd(typeId,typeName){
        var h = '';
        h += '<br>';
        h += '<table cellspacing="0">';
        h += '<tr>';
        h += '<th colspan="2" id="typeSpan'+typeId+'">';
        h += '<span style="font-size:20px;" onclick="editType('+typeId+')">'+typeName+'</span>';
        h += '<input type="hidden" id="typeName'+typeId+'" value="'+typeName+'">';
        h += '</th>';
        h += '</tr>';
        h += '</table>';
        h += '<table cellspacing="0" id="tableList'+typeId+'">';
        h += '</table>';
        h += '<table cellspacing="0">';
        h += '<tr>';
        h += '<td>';
        h += '<input type="text" id="nameNew'+typeId+'" placeholder="新名称"/>';
        h += '</td>';
        h += '<td class="right">';
        h += '<input type="text" id="numNew'+typeId+'" placeholder="新单价"/>';
        h += '</td>';
        h += '</tr>';
        h += '<tr> <th colspan="2" style="text-align:center;color:#0088cc;" onclick="addRow('+typeId+')">保存并插入新行 </th> </tr>';
        h += '</table>';
        $('#pop').hide();
        $('#tlist').append(h);
    }
    /**
     * 增加新行
     * */
    function addRow(typeId){
        var name = $('#nameNew'+typeId).val();
        var num = $('#numNew'+typeId).val();
        if (name=='') {
            getAppErrMsg('新名称未填写!','',1000);
            return;
        }
        if (num=='') {
            getAppErrMsg('新价格未填写!','',1000);
            return;
        }
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            typeId:typeId,
            name:name,
            num:num
        };
        $.ajax({
            url: getDomain() + 'fabric/add',
            type:'post',
            data:data,
            success: function (res) {
//                console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                var h = '', data = res.data;
                h += '<tr>';
                h += '<td><input type="text" id="subName_'+data.costId+'" value="'+name+'" readonly onclick="editCostName('+typeId+','+data.costId+')"/></td>';
                h += '<td><input type="text" id="subNum_'+data.costId+'" value="'+num+'" readonly  onclick="editCostNum('+typeId+','+data.costId+')"/><input type="hidden" id="typeId_'+data.costId+'" value="'+typeId+'"/></td>';
                h += '</tr>';
                getAppErrMsg('新增成功!','',1000);
                setTimeout(function() {
                    $('#tableList'+typeId).append(h);
                    $('#nameNew'+typeId)[0].value = '';
                    $('#numNew'+typeId)[0].value = '';
                }, 1000)
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 编辑面料name
     * */
    function setCostName(costId){
        var typeId = $('#typeId_'+costId).val();
        var subName = $('#subName').val();
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        if (subName=='') {
            getAppErrMsg('该面料名称未填写!','',1000);
            return;
        }
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            typeId:typeId,
            costId:costId,
            subName:subName
        };
        $.ajax({
            type:'post',
            url: getDomain() + 'fabric/modify-name',
            data:data,
            success: function (res) {
//                console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                getAppErrMsg('更新成功！','',0);
                setTimeout(function() {
                    $('#subName_'+costId)[0].value = subName;
                    $('#pop').hide();
                }, 1000)
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 编辑面料cost
     * */
    function setCostNum(costId){
        var typeId = $('#typeId_'+costId).val();
        var subNum = $('#subNum').val();
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        if (subNum=='') {
            getAppErrMsg('该面料名称未填写!','',1000);
            return;
        }
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            costId:costId,
            num:subNum
        };
        $.ajax({
            type:'post',
            url: getDomain() + 'fabric/modify-cost',
            data:data,
            success: function (res) {
//                console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                getAppErrMsg('更新成功！','',0);
                setTimeout(function() {
                    $('#subNum_'+costId)[0].value = subNum;
                    $('#pop').hide();
                }, 1000)
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 获取面料数据
     * */
    function getTypeList () {
        var userInfo = cacheGet('userInfo');
        if (userInfo==null || userInfo.token=='undefined') {
            getErrMsg('没有登录！',''); return;
        }
        var orderId = $('#orderId').val();
        var data = {
            app_access:app_access,
            token:userInfo.token,
            type:userInfo.type,
            cate:userInfo.cate,
            orderId:orderId
        };
        $.ajax({
            url: getDomain() + 'fabric/typelist',
            type:'post',
            data:data,
            success: function (res) {
//                console.log(res); return;
                if (res.code!=0) {
                    console.log(res.msg); return;
                }
                var i, i2, h = '', data = res.data;
                for (i=0;i<data.length;i++) {
                    h += '<br>';
                    h += '<table cellspacing="0">';
                    h += '<tr>';
                    h += '<th colspan="2">';
//                    h += '<input type="text" id="typeName_'+data[i].id+'" style="width:100%;" placeholder="新表名称" value="'+data[i].name+'">';
                    h += '<span style="font-size:20px;" onclick="editType('+data[i].id+')">'+data[i].name+'</span>';
                    h += '<input type="hidden" id="typeName'+data[i].id+'" value="'+data[i].name+'">';
                    h += '</th>';
                    h += '</tr>';
                    h += '</table>';
                    h += '<table cellspacing="0" id="tableList'+data[i].id+'">';
                    var subData = data[i].subs;
                    if (subData) {
                        for (i2=0;i2<subData.length;i2++) {
                            h += '<tr>';
                            h += '<td>';
                            h += '<input type="text" id="subName_'+subData[i2].subId+'" value="'+subData[i2].subName+'" readonly onclick="editCostName('+subData[i2].subId+')"/>';
                            h += '</td>';
                            h += '<td class="right">';
                            h += '<input type="text" id="subNum_'+subData[i2].subId+'" value="'+subData[i2].subCost+'" readonly onclick="editCostNum('+subData[i2].subId+')"/>';
                            h += '<input type="hidden" id="typeId_'+subData[i2].subId+'" value="'+data[i].id+'"/>';
                            h += '</td>';
                            h += '</tr>';
                        }
                    }
                    h += '</table>';
                    h += '<table cellspacing="0">';
                    h += '<tr>';
                    h += '<td>';
                    h += '<input type="text" id="nameNew'+data[i].id+'" placeholder="新名称"/>';
                    h += '</td>';
                    h += '<td class="right">';
                    h += '<input type="text" id="numNew'+data[i].id+'" placeholder="新单价"/>';
                    h += '</td>';
                    h += '</tr>';
                    h += '<tr> <th colspan="2" style="text-align:center;color:#0088cc;" onclick="addRow('+data[i].id+')">保存并插入新行 </th> </tr>';
                    h += '</table><br>';
                }
                $('#tlist').html(h);
            },
            error:function(){
                console.log('操作失败！'); return;
            }
        });
    }
    /**
     * 接口地址前缀
     * */
    function getDomain(){
        return '/api/home/release/';
    }
</script>
</html>